package it.tcm.TeacherClassMate.action;

import java.util.List;

import it.tcm.TeacherClassMate.beans.OrderMainBean;
import it.tcm.TeacherClassMate.beans.TemplateBean;
import it.tcm.TeacherClassMate.facade.OrderMainManageBase;
import it.tcm.TeacherClassMate.facade.TemplateManageBase;
import it.tcm.TeacherClassMate.util.Util;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class ChooseTemplateAction extends Action {

	// --------------------------------------------------------- Instance Variables

	// --------------------------------------------------------- Methods


	private Logger logger = Logger.getLogger(this.getClass().getName());
	
	/** 
	 * Method execute
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * 
	 */

	public ActionForward execute(
		ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,
		HttpServletResponse response) 
	{		
		 logger.info("CHOOSE TEMPLATE");
		 TemplateManageBase tmb = new TemplateManageBase();
		 if (request.getSession().getAttribute(Util.ORDER_MAIN_OBJECT) != null) {
			 OrderMainBean omb = (OrderMainBean)request.getSession().getAttribute(Util.ORDER_MAIN_OBJECT);
			 request.getSession().removeAttribute(Util.ORDER_MAIN_OBJECT);
			 OrderMainManageBase ommb = new OrderMainManageBase();
			 if (!omb.getCompleted().equals(1)) ommb.delete(omb);
			 logger.info("resetto omb");
		 }
		 List<TemplateBean> list = tmb.getAll();
		 request.setAttribute("templateList", list);
		 return mapping.findForward("choosetemplate"); 
		 
	}
	
	
}